//---------------------------------------------------------------------------

#include <vcl.h>
#include <stdio.h>
#pragma hdrstop
#pragma once

//#include "main.h";
#include "PaintRobot.h";
//#include "hrac.h";
#include "cutoff.h";
#include "alfabeta.h";
#include "user.h";

using namespace std;

#include "Unit2.cpp"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

const int rozmer_bunky = 50;
const TColor clrWhite = clRed;
TColor clrBlack = clBlue;
Coordinates *helpCoord;

//definicie pre uzivatela ale iba v grafickom rezime
AnsiString hrMeno = "Hrac 1";
int level = 0;   //obtiaznost supera level 0 - alfabeta, level 1 cutoff

TForm1 *Form1;
PaintRobot *robot;
int aktHrac = cierny;
int mode = 2;   //hraci mod; 1 hraju pocitace proti sebe; 2 hrac proti pocitacu
bool hrac_tahal = false; //iniduje ci hrac potiahol alebo nie

HrPole hrPole;

CutOffHrac white0(biely);
AlfaBetaHrac black0(cierny);
AlfaBetaHrac white1(biely);
UserHrac black(cierny);

int PlayerPoints(typPole& playground, int player_no) {
        int points = 0;
        for (int i = 0; i < rozmerPola; i++) {
                for (int j = 0; j < rozmerPola; j++) {
                        if (playground[i][j] == player_no) {
                                points++;
                        }
                }
        }

        return points;
}

//hlaska pre vypis vytaza
AnsiString displayWinner(AnsiString win_name) {
        return "Game over. The winner is " + win_name;
}


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------



void __fastcall TForm1::FormCreate(TObject *Sender)
{
        //nezobraz konecnu tabulku vytaza
        Form1->winTbl->Visible = false;

        //timer na zaciatku disabled
        Form1->Timer1->Enabled = false;

        //zobraz nastavovaci panel
        Form1->Panel1->Visible = true;

        //nezobraz panel s hrou
        Form1->Panel2->Visible = false;

        //zrus moznost file otvarania
        Form1->restar1->Enabled = false;
        Form1->Helpme1->Enabled = false;

        //pozicia okna
        Form1->Top = 20;
        Form1->Left = 40;

        //nastav poziciu panela
        Form1->Panel1->Top = 8;
        Form1->Panel1->Left = 8;

        //nastavenie farieb pre vyber
        Form1->actclr->Canvas->Brush->Color = clBlue;
        Form1->actclr->Canvas->Rectangle(0,0,Form1->actclr->Width, Form1->actclr->Height);
        Form1->clrpos1->Canvas->Brush->Color = clBlue;
        Form1->clrpos1->Canvas->Rectangle(0,0,Form1->clrpos1->Width, Form1->clrpos1->Height);
        Form1->clrpos2->Canvas->Brush->Color = clGreen;
        Form1->clrpos2->Canvas->Rectangle(0,0,Form1->clrpos2->Width, Form1->clrpos2->Height);
        Form1->clrpos3->Canvas->Brush->Color = clYellow;
        Form1->clrpos3->Canvas->Rectangle(0,0,Form1->clrpos3->Width, Form1->clrpos3->Height);
        Form1->clrpos4->Canvas->Brush->Color = clBlack;
        Form1->clrpos4->Canvas->Rectangle(0,0,Form1->clrpos4->Width, Form1->clrpos4->Height);

        //prisposob velkost formulara velkosti panela
        Form1->Height = Form1->Panel1->Height + 70;
        Form1->Width = Form1->Panel1->Width + 30;

        //nastav aktualnu vybratu hodnotu
        if (level == 1) {
                Form1->RadioGroup1->ItemIndex = 1;
        }
        else {
                Form1->RadioGroup1->ItemIndex = 0;
        }

        //podla modu hry nastav ktore kolonky maju byt aktivne
        //ak mode == 2 hra aj hrac
        if (mode == 2) {
                Form1->hrname->Enabled = true;
                Form1->actclr->Enabled = true;
                Form1->clrpos1->Enabled = true;
                Form1->clrpos2->Enabled = true;
                Form1->clrpos3->Enabled = true;
                Form1->clrpos4->Enabled = true;
                Form1->RadioGroup1->Enabled = true;
                Form1->RadioGroup2->ItemIndex = 1;
         }
         else {
                Form1->hrname->Enabled = false;
                Form1->actclr->Enabled = false;
                Form1->clrpos1->Enabled = false;
                Form1->clrpos2->Enabled = false;
                Form1->clrpos3->Enabled = false;
                Form1->clrpos4->Enabled = false;
                Form1->RadioGroup1->Enabled = false;                
                Form1->RadioGroup2->ItemIndex = 0;
         }

         robot = NULL;
         hrPole.init();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Exit1Click(TObject *Sender)
{
        Form1->Close();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Image1MouseDown(TObject *Sender,
      TMouseButton Button, TShiftState Shift, int X, int Y)
{
        //hraju hrac vs pocitac
        if (mode == 2) {
                //vypni help me mode
                //Form1->Helpme1->Enabled = false;

                int pX = X/rozmer_bunky;
                int pY = Y/rozmer_bunky;

                if (! hrPole.koniecHry()) {
                        hrac_tahal = false;
                        hrPole.setColor(aktHrac);
                        //ak moze hrac urobit nejaky tah nech ho urobi, ak je nejake policko prazdne
                        if (black.getNumOfPossibleMoves(*hrPole.getField(), aktHrac) > 0) {
                                if (black.checkMove(*hrPole.getField(), pY, pX)) {
                                        black.move(*hrPole.getField(), pY, pX);
                                        //ak sa zmeni hrac z User na PC vypni help tlacidlo
                                        Form1->Helpme1->Enabled = true;
                                        aktHrac = biely;
                                        Form1->hrac1_lb->Font->Color = clRed;
                                        Form1->hrac2_lb->Font->Color = clBlack;
                                        Form1->error->Caption = "";
                                        //urobil tah prekresli hraciu plochu
                                        robot->DrawPlayground(*hrPole.getField(), clrWhite, clrBlack);
                                        //spusti timer pre tah pocitaca
                                        Form1->Timer2->Enabled = true;
                                        Form1->Timer2Timer(Sender);
                                }
                                else {
                                        Form1->error->Caption = "�ah na pol��ko nie je nepovolen�";
                                }
                        }
                        else {
                                Form1->error->Caption = "Nem� kam potiahnu�. �ah� s�per";
                                aktHrac = biely;
                                //spusti timer pre tah pocitaca
                                Form1->Timer2->Enabled = true;
                                Form1->Timer2Timer(Sender);
                        }

                }
                else {
                        int pc_points = PlayerPoints(*hrPole.getField(), 1);
                        int usr_points = PlayerPoints(*hrPole.getField(), 2);
                        if (usr_points > pc_points) {
                                Form1->winmsg->Caption = displayWinner(Form1->hrac2_lb->Caption);
                                Form1->winTbl->Visible = true;
                        }
                        else {
                                Form1->winmsg->Caption = displayWinner(Form1->hrac1_lb->Caption);
                                Form1->winTbl->Visible = true;
                        }
                }

                //vypocet bodov pre jendotlivych hracov; druha premenna je cislo hraca
                Form1->hr1_points->Caption = PlayerPoints(*hrPole.getField(), 1);
                Form1->hr2_points->Caption = PlayerPoints(*hrPole.getField(), 2);
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Helpme1Click(TObject *Sender)
{
        helpCoord = black.getMoves(*hrPole.getField(), aktHrac);
        if (robot != NULL) {
                robot->helpMoves(helpCoord);
        }
        if (helpCoord != NULL) {
                helpCoord->freeCoordinates();
                delete helpCoord;
        }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::restar1Click(TObject *Sender)
{
         //skry winner tabulky
         Form1->winTbl->Visible = false;

         robot->VycistiPlochu();
         //hrac cislo 1 je aj zacinajuci hrac
         Form1->hrac1_lb->Font->Color = clGreen;
         Form1->hr1_points->Caption = 2;
         //hrac cislo 1 je aj zacinajuci hrac
         Form1->hrac2_lb->Font->Color = clBlack;
         Form1->hr2_points->Caption = 2;

         //nastav aktivneho hraca na biely
         aktHrac = biely;

         robot = NULL;
         robot = new PaintRobot(Form1->Image1, rozmer_bunky, rozmerPola);
         robot->Mriezka(); //vykreslenie mriezky

         hrPole.init();

         robot->DrawPlayground(*hrPole.getField(), clrWhite, clrBlack);

         //ak bol nastaveny mode 1 hraju dva pocitace tak spusti hru cez casovac
         if (mode == 1) {
                //vypni help me mode
                Form1->Helpme1->Enabled = false;
                Form1->Timer1->Enabled = true;
                Form1->Timer1Timer(Sender);
         }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::startClick(TObject *Sender)
{
        //nezobraz konecnu tabulku vytaza
        Form1->winTbl->Visible = false;

        //nezobraz nastavenie
        Form1->Panel1->Visible = false;
        //zobraz hraciu plochu
        Form1->Panel2->Visible = true;
        //nastav povodnu velkost formulara
        Form1->Height = Form1->Panel2->Height + 70;
        Form1->Width = Form1->Panel2->Width + 25;

        //zobraz tlacitka vo file menu
        Form1->restar1->Enabled = true;
        Form1->Helpme1->Enabled = true;

        //farba hraca cislo 1
        Form1->hr1cl->Canvas->Brush->Color = clrWhite;
        Form1->hr1cl->Canvas->Rectangle(0,0,Form1->hr1cl->Width, Form1->hr1cl->Height);
        if (aktHrac == biely) {
                Form1->hrac1_lb->Font->Color = clRed;
                Form1->hrac2_lb->Font->Color = clBlack;
        }
        else {
                Form1->hrac2_lb->Font->Color = clRed;
                Form1->hrac1_lb->Font->Color = clBlack;
        }

        //ak je zaskrtnuty vyber hrac proti pc tak nastav meno hraca
        //farbu hraca a obtiaznost podla vyberu
        if (mode == 2) {
                //nastav meno hraca
                hrMeno = Form1->hrname->Text;
                Form1->hrac2_lb->Caption = hrMeno;

                //nastav farbu hraca
                clrBlack = Form1->actclr->Canvas->Brush->Color;
                Form1->hr2cl->Canvas->Brush->Color = clrBlack;
                Form1->hr2cl->Canvas->Rectangle(0, 0, Form1->hr2cl->Width, Form1->hr2cl->Height);

                //nastav body hracom
                Form1->hr1_points->Caption = 2;
                Form1->hr2_points->Caption = 2;


                if (Form1->RadioGroup1->ItemIndex == 0) {
                        level = 0;
                        Form1->hrac1_lb->Caption = "CutOff";
                }
                else {
                        level = 1;
                        Form1->hrac1_lb->Caption = "AlfaBeta";

                }

        }
        //ak je vybrana hra pc vs. pc tak nastave len mena ktory pc je ktroy robot a hraj
        else {
                Form1->hrac1_lb->Caption = "CutOff";
                Form1->hr1_points->Caption = "2";
                Form1->hrac2_lb->Caption = "AlfaBeta";
                Form1->hr2_points->Caption = "2";
                //nastav farbu kamenov druheho hraca; prvy je standard cerveny :)
                Form1->hr2cl->Canvas->Brush->Color = clrBlack;
                Form1->hr2cl->Canvas->Rectangle(0, 0, Form1->hr2cl->Width, Form1->hr2cl->Height);

        }

        //zmaz plochu
        if (robot != NULL) {
                robot->VycistiPlochu();
        }

         robot = NULL;
         robot = new PaintRobot(Form1->Image1, rozmer_bunky, rozmerPola);
         robot->Mriezka(); //vykreslenie mriezky

         hrPole.init();

         robot->DrawPlayground(*hrPole.getField(), clrWhite, clrBlack);

         //ak je nastaveny mode 1 .. t.j. hraju dva pc proti sebe spusti timer
         if (mode == 1) {
                //vypni help me mode
                Form1->Helpme1->Enabled = false;
                Form1->Timer1->Enabled = true;
                Form1->Timer1Timer(Sender);
         }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::N1Click(TObject *Sender)
{
        //skry winner tabulky
        Form1->winTbl->Visible = false;

        //skry hraciu plochu aj navigaciu
        Form1->Panel2->Visible = false;
        //zobraz nastavenia
        Form1->Panel1->Visible = true;

        //nastavenie farieb pre vyber
        Form1->actclr->Canvas->Brush->Color = clBlue;
        Form1->actclr->Canvas->Rectangle(0,0,Form1->actclr->Width, Form1->actclr->Height);
        Form1->clrpos1->Canvas->Brush->Color = clBlue;
        Form1->clrpos1->Canvas->Rectangle(0,0,Form1->clrpos1->Width, Form1->clrpos1->Height);
        Form1->clrpos2->Canvas->Brush->Color = clGreen;
        Form1->clrpos2->Canvas->Rectangle(0,0,Form1->clrpos2->Width, Form1->clrpos2->Height);
        Form1->clrpos3->Canvas->Brush->Color = clYellow;
        Form1->clrpos3->Canvas->Rectangle(0,0,Form1->clrpos3->Width, Form1->clrpos3->Height);
        Form1->clrpos4->Canvas->Brush->Color = clBlack;
        Form1->clrpos4->Canvas->Rectangle(0,0,Form1->clrpos4->Width, Form1->clrpos4->Height);

        //nastav poziciu panela
        Form1->Panel1->Top = 8;
        Form1->Panel1->Left = 8;

        //prisposob velkost formulara velkosti panela
        Form1->Height = Form1->Panel1->Height + 70;
        Form1->Width = Form1->Panel1->Width + 30;

        //nastav aktualnu vybratu hodnotu
        if (level == 1) {
                Form1->RadioGroup1->ItemIndex = 1;
        }
        else {
                Form1->RadioGroup1->ItemIndex = 0;
        }

        //podla modu hry nastav ktore kolonky maju byt aktivne
        //ak mode == 2 hra aj hrac
        if (mode == 2) {
                Form1->hrname->Enabled = true;
                Form1->actclr->Enabled = true;
                Form1->clrpos1->Enabled = true;
                Form1->clrpos2->Enabled = true;
                Form1->clrpos3->Enabled = true;
                Form1->clrpos4->Enabled = true;
                Form1->RadioGroup1->Enabled = true;
                Form1->RadioGroup2->ItemIndex = 1;
         }
         else {
                Form1->hrname->Enabled = false;
                Form1->actclr->Enabled = false;
                Form1->clrpos1->Enabled = false;
                Form1->clrpos2->Enabled = false;
                Form1->clrpos3->Enabled = false;
                Form1->clrpos4->Enabled = false;
                Form1->RadioGroup1->Enabled = false;                
                Form1->RadioGroup2->ItemIndex = 0;
         }
}
//---------------------------------------------------------------------------

void __fastcall TForm1::clrpos1Click(TObject *Sender)
{
        clrBlack = Form1->clrpos1->Canvas->Brush->Color;
        Form1->actclr->Canvas->Brush->Color = clrBlack;
        Form1->actclr->Canvas->Rectangle(0,0,Form1->actclr->Width,Form1->actclr->Height);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::clrpos2Click(TObject *Sender)
{
        clrBlack = Form1->clrpos2->Canvas->Brush->Color;
        Form1->actclr->Canvas->Brush->Color = clrBlack;
        Form1->actclr->Canvas->Rectangle(0,0,Form1->actclr->Width,Form1->actclr->Height);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::clrpos3Click(TObject *Sender)
{
        clrBlack = Form1->clrpos3->Canvas->Brush->Color;
        Form1->actclr->Canvas->Brush->Color = clrBlack;
        Form1->actclr->Canvas->Rectangle(0,0,Form1->actclr->Width,Form1->actclr->Height);
}
//---------------------------------------------------------------------------

void __fastcall TForm1::clrpos4Click(TObject *Sender)
{
        clrBlack = Form1->clrpos4->Canvas->Brush->Color;
        Form1->actclr->Canvas->Brush->Color = clrBlack;
        Form1->actclr->Canvas->Rectangle(0,0,Form1->actclr->Width,Form1->actclr->Height);
}
//---------------------------------------------------------------------------


void __fastcall TForm1::About1Click(TObject *Sender)
{
        Form2->ShowModal();        
}
//---------------------------------------------------------------------------



void __fastcall TForm1::RadioGroup2Click(TObject *Sender)
{
        if (Form1->RadioGroup2->ItemIndex == 0) {
                Form1->hrname->Enabled = false;
                Form1->actclr->Enabled = false;
                Form1->clrpos1->Enabled = false;
                Form1->clrpos2->Enabled = false;
                Form1->clrpos3->Enabled = false;
                Form1->clrpos4->Enabled = false;
                Form1->RadioGroup1->Enabled = false;

                mode = 1;
        }
        else {
                Form1->hrname->Enabled = true;
                Form1->actclr->Enabled = true;
                Form1->clrpos1->Enabled = true;
                Form1->clrpos2->Enabled = true;
                Form1->clrpos3->Enabled = true;
                Form1->clrpos4->Enabled = true;
                Form1->RadioGroup1->Enabled = true;

                mode = 2;
        }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::RadioGroup1Click(TObject *Sender)
{
        if (Form1->RadioGroup1->ItemIndex == 0) {
                level = 0;
        }
        else {
                level = 1;
        }
}
//---------------------------------------------------------------------------





void __fastcall TForm1::Timer1Timer(TObject *Sender)
{     

        //ak je nastaveny mode na 1c hraju dva pc proti sebe ake je nastaveny iny mod urob
        //len automaticky tahj za jeden pc podla zvolenej obtiaznosti
        if (mode == 1) {
           if (! hrPole.koniecHry()) {
                if (aktHrac == biely) {
                    hrPole.setColor(aktHrac);
                    white0.move(*hrPole.getField(),0,0);
                    aktHrac = cierny;
                    Form1->hrac2_lb->Font->Color = clRed;
                    Form1->hrac1_lb->Font->Color = clBlack;
                }
                else {
                    hrPole.setColor(aktHrac);
                    black0.move(*hrPole.getField(),0,0);
                    aktHrac = biely;
                    Form1->hrac1_lb->Font->Color = clRed;
                    Form1->hrac2_lb->Font->Color = clBlack;
                }
            }
           else {
                int pc1_points = PlayerPoints(*hrPole.getField(), 1);
                int pc2_points = PlayerPoints(*hrPole.getField(), 2);
                if (pc1_points < pc2_points) {
                        Form1->winmsg->Caption = displayWinner(Form1->hrac2_lb->Caption);
                        Form1->winTbl->Visible = true;
                }
                else {
                     Form1->winmsg->Caption = displayWinner(Form1->hrac2_lb->Caption);
                     Form1->winTbl->Visible = true;
                }
                Form1->Timer1->Enabled = false;
           }

           robot->DrawPlayground(*hrPole.getField(), clrWhite, clrBlack);
           //vypocet bodov pre jendotlivych hracov; druha premenna je cislo hraca
           Form1->hr1_points->Caption = PlayerPoints(*hrPole.getField(), 1);
           Form1->hr2_points->Caption = PlayerPoints(*hrPole.getField(), 2);
        }
        
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
        Form1->N1->Click();        
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
        Form1->restar1->Click();        
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button3Click(TObject *Sender)
{
        Form1->Close();        
}
//---------------------------------------------------------------------------




void __fastcall TForm1::Timer2Timer(TObject *Sender)
{
        //prvy interval musi ist naprazdno a v nom nastav ze hrac potiahol
        if (!hrac_tahal) {
                hrac_tahal = true;
        }
        //druhy interval urobi tah pocitaca a zastavi timer
        else {
                if (level == 1) {
                        hrPole.setColor(aktHrac);
                        white0.move(*hrPole.getField(),0,0);
                }
                else {
                        hrPole.setColor(aktHrac);
                        white1.move(*hrPole.getField(),0,0);
                }
                aktHrac = cierny;
                //ak sa zmeni hrac z PC na User zapni help tlacidlo
                Form1->Helpme1->Enabled = true;
                Form1->hrac2_lb->Font->Color = clRed;
                Form1->hrac1_lb->Font->Color = clBlack;
                Form1->error->Caption = "";

                //prekresli hraciu plochu
                robot->DrawPlayground(*hrPole.getField(), clrWhite, clrBlack);

                Form1->Timer2->Enabled = false;

                //skontroluj ci moze este aspon jeden hrac niekam potiahnut
                int black_moves;
                int white_moves;

                black_moves = black.getNumOfPossibleMoves(*hrPole.getField(), cierny);
                if (level == 1) { white_moves = white0.getNumOfPossibleMoves(*hrPole.getField(), biely);}
                else {white_moves = white1.getNumOfPossibleMoves(*hrPole.getField(), biely);}

                //ak nemoze tahat ani jeden z hracov ukonci hru
                if ((black_moves == 0) && (white_moves == 0)) {
                        int pc_points = PlayerPoints(*hrPole.getField(), 1);
                        int usr_points = PlayerPoints(*hrPole.getField(), 2);
                        if (usr_points > pc_points) {
                                Form1->winmsg->Caption = displayWinner(Form1->hrac2_lb->Caption);
                                Form1->winTbl->Visible = true;
                        }
                        else {
                                Form1->winmsg->Caption = displayWinner(Form1->hrac1_lb->Caption);
                                Form1->winTbl->Visible = true;
                        }
                }
        }

        Form1->hr2_points->Caption = PlayerPoints(*hrPole.getField(), 2);

}
//---------------------------------------------------------------------------

